package com.meeting.meet.manager.impl.base;

import cn.hutool.core.collection.CollectionUtil;

import com.meeting.meet.manager.AgendaAndAttendeeDtoManager;
import com.meeting.meet.manager.AgendaBaseDtoManager;
import com.meeting.meet.manager.converter.AgendaAndAttendeeDtoConverter;
import com.meeting.meet.manager.converter.AgendaBaseDtoConverter;
import com.meeting.meet.manager.dto.AgendaAndAttendeeDto;
import com.meeting.meet.manager.dto.AgendaBaseDto;
import com.meeting.meet.persist.dos.Agenda;
import com.meeting.meet.persist.mapper.AgendaDao;
import com.vs.code.AutoGenerated;
import com.vs.ox.common.exception.ErrorCode;
import com.vs.ox.common.exception.IgnoredException;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@AutoGenerated(locked = true, uuid = "570f4cfc-d7b5-3aa6-bd9b-6f7f537278e9")
public abstract class AgendaAndAttendeeDtoManagerBaseImpl implements AgendaAndAttendeeDtoManager {
    @AutoGenerated(locked = true)
    @Autowired
    private AgendaAndAttendeeDtoConverter agendaAndAttendeeDtoConverter;

    @AutoGenerated(locked = true)
    @Autowired
    private AgendaBaseDtoConverter agendaBaseDtoConverter;

    @AutoGenerated(locked = true)
    @Autowired
    private AgendaBaseDtoManager agendaBaseDtoManager;

    @AutoGenerated(locked = true)
    @Autowired
    private AgendaDao agendaDao;

    @AutoGenerated(locked = true, uuid = "47c5586c-a522-3fda-aaeb-f9860d63cb93")
    @Override
    public List<AgendaAndAttendeeDto> getByIds(List<Long> id) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(id)) {
            return Collections.emptyList();
        }

        List<Agenda> agendaList = agendaDao.getByIdsForAgendaAndAttendeeDto(id);
        if (CollectionUtil.isEmpty(agendaList)) {
            return Collections.emptyList();
        }

        // 结果和入参顺序保持一致
        Map<Long, Agenda> agendaMap =
                agendaList.stream().collect(Collectors.toMap(i -> i.getId(), i -> i));
        agendaList =
                id.stream()
                        .map(i -> agendaMap.get(i))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());

        return doConvertFromAgendaToAgendaAndAttendeeDto(agendaList);
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "50e948db-e4cd-3a01-a38c-a1a2fc2b0128")
    @Override
    public List<AgendaAndAttendeeDto> getByMeetIds(List<Long> meetId) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(meetId)) {
            return Collections.emptyList();
        }

        List<Agenda> agendaList = agendaDao.getByMeetIdsForAgendaAndAttendeeDto(meetId);
        if (CollectionUtil.isEmpty(agendaList)) {
            return Collections.emptyList();
        }

        return doConvertFromAgendaToAgendaAndAttendeeDto(agendaList);
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "9aaf8b55-8f3f-3371-8f1e-a45c85b72731")
    public List<AgendaAndAttendeeDto> doConvertFromAgendaToAgendaAndAttendeeDto(
            List<Agenda> agendaList) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(agendaList)) {
            return Collections.emptyList();
        }

        List<AgendaBaseDto> baseDtoList =
                agendaList.stream()
                        .map(tmp -> agendaBaseDtoConverter.convertFromAgendaToAgendaBaseDto(tmp))
                        .collect(Collectors.toList());
        Map<Long, AgendaAndAttendeeDto> dtoMap =
                agendaAndAttendeeDtoConverter
                        .convertFromAgendaBaseDtoToAgendaAndAttendeeDto(baseDtoList)
                        .stream()
                        .collect(
                                Collectors.toMap(
                                        AgendaAndAttendeeDto::getId,
                                        Function.identity(),
                                        (o1, o2) -> o1));

        List<AgendaAndAttendeeDto> agendaAndAttendeeDtoList = new ArrayList<>();
        for (Agenda i : agendaList) {
            AgendaAndAttendeeDto agendaAndAttendeeDto = dtoMap.get(i.getId());
            if (agendaAndAttendeeDto == null) {
                continue;
            }

            /** This block is generated by vs, do not modify, end anchor 1 */

            /** This block is generated by vs, do not modify, start anchor 2 */
            agendaAndAttendeeDtoList.add(agendaAndAttendeeDto);
        }
        return agendaAndAttendeeDtoList;
        /** This block is generated by vs, do not modify, end anchor 2 */
    }

    @AutoGenerated(locked = true, uuid = "c2c609ae-a1b0-33ad-95df-a80626c5a09e")
    @Override
    public List<AgendaAndAttendeeDto> getByMeetId(Long meetId) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        List<AgendaAndAttendeeDto> agendaAndAttendeeDtoList = getByMeetIds(Arrays.asList(meetId));
        return agendaAndAttendeeDtoList;
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "cda0a8d0-da64-3b82-83ec-be4d887d3e06")
    @Override
    public AgendaAndAttendeeDto getById(Long id) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        List<AgendaAndAttendeeDto> ret = getByIds(Arrays.asList(id));
        if (ret.size() > 1) {
            throw new IgnoredException(ErrorCode.SYS_ERROR, "返回值个数超过1个");
        }
        AgendaAndAttendeeDto agendaAndAttendeeDto = CollectionUtil.isEmpty(ret) ? null : ret.get(0);
        return agendaAndAttendeeDto;
        /** This block is generated by vs, do not modify, end anchor 1 */
    }
}
